<template>
    <div>
        <div class="list">
            <div class="item" v-for="(item, index) in list" :key="index">
                <articleItem :item="item" type="draft"></articleItem>
                <div class="btns flex-v-center">
                    <div class="btn flex-1 flex-center" @click="deleteItem(item)">
                        <i class="icon-delete3"></i>
                        删除
                    </div>
                    <div class="btn flex-1 flex-center" @click="editItem(item)">
                        <i class="icon-bianji"></i>
                        继续编辑
                    </div>
                </div>
            </div>
        </div>
        <pub-list-end :loading="scroll.loading" :max="scroll.max" :noData="scroll.noData"></pub-list-end>
    </div>
</template>

<script>
import articleItem from '@/components/pages/article-item.vue'
import { getDynamicDraft, editDynamicDraft } from '@/request/art'
export default {
    components: {
        articleItem
    },
    data() {
        return {
            list: [],
            scroll: {
                page: 1,
                size: 30,
                loading: false,
                max: false,
                noData: false,
            },
        }
    },
    onLoad() {
    },
    onShow() {
        this.onRefresh()
    },
    onReachBottom() {
        this.onInfinite()
    },
    methods: {
        getList(complete) {
            getDynamicDraft({
                pageIndex: this.scroll.page,
                pageSize: this.scroll.size,
            }, { complete }).then(res => {
                this.list = this.scroll.page == 1 ? res.data.list : this.list.concat(res.data.list)
                this.scroll.max = this.list.length >= res.data.total
                this.scroll.noData = res.data.total == 0
            })
        },
        // 重头第一页刷新列表
        onRefresh() {
            this.scroll.loading = true
            this.scroll.max = false
            this.scroll.noData = false
            this.scroll.page = 1
            this.list = []
            this.getList(() => {
                this.scroll.loading = false;
                uni.stopPullDownRefresh()
            })
        },
        // 滚动到底部加载
        onInfinite() {
            if(this.scroll.loading || this.scroll.max) return
            this.scroll.loading = true
            this.scroll.page += 1
            this.getList(() => {
                this.scroll.loading = false
            })
        },
        deleteItem(item){
            this.$interact.modal({
                title: '提示',
                content: '是否删除草稿？',
                showCancel: true,
            }).then(action => {
                if(action.confirm) {
                    editDynamicDraft({
                        id: item.id,
                        delStatus: '01'
                    }).then(res => {
                        this.$interact.toast(res.msg)
                    })
                }
            })
        },
        editItem(item) {
            this.$navigate.to('/pages/create/write?draftId=' + item.id)
        }

    }

}
</script>

<style lang="less" scoped>

.list{
    >.item{
        padding: 10rpx 20rpx;
        background: #fff;
        margin-bottom: 20rpx;
        .btns{
            height: 90rpx;
            .btn{
                border-right: 1px solid #ddd;
                &:last-child{
                    border: 0;
                }
                i{
                    margin-right: 10rpx;
                }
            }

        }
    }
}


</style>